package com.eci.context.context.component;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext = null;

	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		applicationContext = context;
	}

	/**
	 * 获取applicationContext对象
	 * 
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 根据beanName来查找对象
	 * 
	 * @param beanName
	 * @return
	 */

	public static Object getBeanByName(String beanName) {
		
		return applicationContext.getBean(beanName);
	}

	/**
	 * 根据bean的class来查找对象
	 * 
	 * @param clazz
	 * @return
	 */
	public static <T> T getBeanByClass(Class<T> clazz) {
		return (T) applicationContext.getBean(clazz);
	}
	
	
	/**
	 * 根据beanName来查找对象
	 * 
	 * @param beanName
	 * @param clazz
	 * @return
	 */

	public static <T> T getBean(String beanName,Class<T> clazz) {
		
		return (T)applicationContext.getBean(beanName, clazz);
	}
	
	
	

}
